package presstrack
{
	import flash.net.Responder;
	
	import mx.collections.ArrayCollection;
	import mx.utils.ObjectUtil;
	
	import org.presstrack.service.*;
	
	[RemoteClass(alias="presstrack.PaperClass")]
	[Bindable]
	public class PaperClass
	{
		public var iPaperId		:int;
		public var mLabel		:String;
		public var iThick		:int;
		public var mSize		:String;
		public var mCost		:String;
		public var iTotal		:int;
		public var iNumOfReq	:int;
		public var iNumOfDel	:int;		
		public var _arDelivery	:Array 				= new Array();
		public var arDelivery	:ArrayCollection	= new ArrayCollection();
		public var _arIssue		:Array 				= new Array();
		public var arIssue		:ArrayCollection	= new ArrayCollection();
		
		public function PaperClass(ob:PaperClass = null)
		{
			if (ob != null)
			{
				copyFrom(ob);
			}
		}
		
		public function copyFrom(ob:PaperClass):void
		{
			iPaperId	= ob.iPaperId;				
			mLabel		= ob.mLabel.toString();				
			iThick		= ob.iThick;
			mSize		= ob.mSize.toString();
			mCost		= Number(ob.mCost).toFixed(2);			
			iTotal		= ob.iTotal;
			iNumOfReq	= 0;
			iNumOfDel	= 0;
			
			for each (var del:Object in ob._arDelivery)
			{
				var delivery:PaperDeliveryClass = new PaperDeliveryClass(del);
				delivery.mPaperLabel	= mLabel;
				delivery.iPaperThick	= iThick;
				delivery.mPaperSize		= mSize;			
				arDelivery.addItem(delivery);
				CrudService.arDelivery.addItem(delivery);
			}
			
			arDelivery.filterFunction = CrudService.crud.filterForDeliveryInStore;
			arDelivery.refresh();
			CrudService.arDelivery.filterFunction = CrudService.crud.filterForDelivery;
			CrudService.arDelivery.refresh();
			
			for each (var iss:Object in ob._arIssue)
			{
				var issue:PaperIssueClass = new PaperIssueClass(iss);
				issue.mPaperLabel	= mLabel;
				issue.iPaperThick	= iThick;
				issue.mPaperSize	= mSize;			
				arIssue.addItem(issue);
				CrudService.arIssue.addItem(issue);
			}
			
			arIssue.filterFunction = CrudService.crud.filterForIssueInStore;
			arIssue.refresh();
			CrudService.arIssue.filterFunction = CrudService.crud.filterForIssue;
			CrudService.arIssue.refresh();
		}
		
		public function setLabel(value		:String, 
								 resFunc	:Function	= null, 
								 faultFunc	:Function	= null):void
		{
			if (value != mLabel)
			{
				updateParam("label", value, onRes, faultFunc);
			}
			
			function onRes(res:String):void 
			{
				if (res == "successful") 
				{
					mLabel = value;
					if (resFunc != null)
					{
						resFunc();
					}
				} 
				else 
				{
					if (faultFunc != null)
					{
						faultFunc(res);
					}
				}
			}
		}
		
		public function setThick(value		:String, 
								 resFunc	:Function	= null, 
								 faultFunc	:Function	= null):void
		{
			if (value != iThick.toString())
				updateParam("thick", value, onRes, faultFunc);
			
			function onRes(res:String):void {
				if (res == "successful") {
					iThick = int(value);
					if (resFunc != null) resFunc();
				} else {
					if (faultFunc != null) faultFunc(res);
				}
			}
		}
		
		public function setSize(value		:String, 
								resFunc		:Function	= null, 
								faultFunc	:Function	= null):void
		{
			if (value != mSize)
				updateParam("size", value, onRes, faultFunc);
			
			function onRes(res:String):void {
				if (res == "successful") {
					mSize = value;
					if (resFunc != null) resFunc();
				} else {
					if (faultFunc != null) faultFunc(res);
				}
			}
		}
		
		public function setCost(value		:String, 
								resFunc		:Function	= null, 
								faultFunc	:Function	= null):void
		{
			if (value != mCost)
				updateParam("cost", value, onRes, faultFunc);
			
			function onRes(res:String):void {
				if (res == "successful") {
					mCost = Number(value).toFixed(2);
					if (resFunc != null) resFunc();
				} else {
					if (faultFunc != null) faultFunc(res);
				}
			}
		}
		
		public function setTotal(value		:String, 
								 resFunc	:Function	= null, 
								 faultFunc	:Function	= null):void
		{
			if (value != iTotal.toString())
				updateParam("total", value, onRes, faultFunc);
			
			function onRes(res:String):void {
				if (res == "successful") {
					iTotal = int(value);
					if (resFunc != null) resFunc();
				} else {
					if (faultFunc != null) faultFunc(res);
				}
			}
		}
		
		public function correctTotal(value			:String, 
									 correct_label	:String, 
									 resFunc		:Function	= null, 
									 faultFunc		:Function	= null):void
		{
			if (value != iTotal.toString())
				CrudService.crud.srv.call("PressTrack.correctPaper", new Responder( onRes, onFault ), iPaperId, value, correct_label, CrudService.crud.getCurrentDate() );
			
			function onRes(result:Object):void 
			{
				if (result.status == true) {
					iTotal = int(value);
					if(result.hasOwnProperty("issue"))
					{
						var issue:PaperIssueClass = new PaperIssueClass(result.issue);
						CrudService.arIssue.addItem(issue);
						CrudService.arIssue.refresh();
					}
					
					if(result.hasOwnProperty("delivery"))
					{
						var delivery:PaperDeliveryClass = new PaperDeliveryClass(result.delivery);
						CrudService.arDelivery.addItem(delivery);
						CrudService.arDelivery.refresh();
					}
					
					if (resFunc != null)
					{
						resFunc();
					}
				}
				else if(faultFunc != null) faultFunc(result.message);
			}
			
			function onFault(e:Object) : void {
				faultFunc(ObjectUtil.toString(e));
			}
		}
		
		public function addIssue(issue		:PaperIssueClass, 
								 resFunc	:Function 			= null, 
								 faultFunc	:Function			= null):void 
		{
			CrudService.crud.srv.call("PressTrack.AddIssue", new Responder(onResult, onFault), 
					 				  iPaperId, issue.iNum, issue.mCost, 
					 				  issue.mCreationDate, issue.iRequest, issue.mLabel);	
			
			function onResult(result:Object):void
			{
				if (result.status == true) 
				{
					issue.iIssueId 		= Number(result.idissue);
					issue.iPaperId		= iPaperId;
					issue.mPaperLabel 	= mLabel;
					issue.iPaperThick 	= iThick;
					issue.mPaperSize 	= mSize;
					arIssue.addItem(issue);	
					CrudService.arIssue.addItem(issue);
					CrudService.arIssue.refresh();
					iTotal = result.paper_total;
					CrudService.arPaper.refresh();
					if (resFunc != null)
					{
						resFunc();
					}
				}
				else
				{
					faultFunc(result.message);
				}
			}
			
			function onFault(e:Object):void 
			{
				faultFunc(ObjectUtil.toString(e));
			}		
		}
		
		public static function refreshNumOfReq():void
		{
			for each (var paper:PaperClass in CrudService.arPaper)
			{	
				paper.iNumOfReq = 0;
			}
			
			for each (var _req:Object in CrudService.arCurRequest)
			{
				for each (var paper:PaperClass in CrudService.arPaper)
				{	
					if (_req.paper.paper_label	== paper.mLabel
					 && _req.paper.paper_thick	== paper.iThick
					 && _req.paper.paper_size	== paper.mSize)		
					{
						paper.iNumOfReq += _req.total;
					}				
				}
			}
			
			for each (var _req:Object in CrudService.arAllRequest)
			{
				if (_req.paper.state == 2) // если бумага по заявке заказана 
				{
					for each (var paper:PaperClass in CrudService.arPaper)
					{	
						if (_req.paper.paper_label	== paper.mLabel
					   	 && _req.paper.paper_thick	== paper.iThick
					 	 && _req.paper.paper_size	== paper.mSize)		
						{
					 		paper.iNumOfReq += _req.total;
						}
					}				
				}
			}
		}
		
		public static function refreshNumOfDel():void
		{
			for each (var paper:PaperClass in CrudService.arPaper)
			{	
				paper.iNumOfDel = 0;
			}
			/*for each (var _req:Object in CrudService.arAllRequest)
			{
				if (_req.paper.state == 2)
				{
					for each (var paper:PaperClass in CrudService.arPaper)
					{	
						if (_req.paper.paper_label	== paper.mLabel
					 	&& _req.paper.paper_thick	== paper.iThick
					 	&& _req.paper.paper_size	== paper.mSize)		
						{
					 		paper.iNumOfReq += _req.total;
					 		paper.iNumOfDel += _req.total;
						}
					}				
				}
			}*/
			
			for each (var paper:PaperClass in CrudService.arPaper)
			{
				paper.arIssue.filterFunction = null;
				paper.arIssue.refresh();
				
				for each (var iss:PaperIssueClass in paper.arIssue)
				{
					if (iss.mCreationDate == "??/??/????")
					{
						paper.iNumOfDel += iss.iNum;
					}
				}		
				
				CrudService.arIssue.filterFunction = CrudService.crud.filterForIssue;
				CrudService.arIssue.refresh();	
			}
		}
		
		// private
		private function updateParam(param		:String, 
									 value		:String, 
									 onResult	:Function	= null,
									 faultFunc	:Function	= null):void 
		{
			CrudService.crud.srv.call("PressTrack.updatePaper", new Responder( onResult, onFault ), iPaperId, param, value);
									
			function onFault(e:Object) : void {
				faultFunc(ObjectUtil.toString(e));
			}			
		}		
	}
}